Replace old endianness logic in binary formats
authorrobertl <robertl@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Sun, 21 Dec 2003 20:30:54 +0000 (20:30 +0000)
committerrobertl <robertl@f51c46e8-681c-474f-0cfe-069cfd0219fb>
Sun, 21 Dec 2003 20:30:54 +0000 (20:30 +0000)
with newer stuff.
Fix loops that free ll elements.
Misc cleanups.

gpsbabel/duplicate.c
gpsbabel/garmin_tables.h
gpsbabel/main.c
gpsbabel/mapsend.c
gpsbabel/mapsource.c
gpsbabel/navicache.c
gpsbabel/polygon.c
gpsbabel/psp.c
gpsbabel/route.c
gpsbabel/tpg.c
gpsbabel/waypt.c

index a7846e40bcc32275b14ee6d287bd52549cd07a67..e9213ad5d8245eb9d693bbf6b8bc85e50814bf03 100644 (file)
@@ -109,7 +109,7 @@ get_crc32(void * data, int datalen)
 static btree_node *
 addnode (btree_node * tree, btree_node * newnode, btree_node **oldnode)
 {
-       btree_node * tmp, * last;
+       btree_node * tmp, * last = NULL;
 
        if ( *oldnode ) {*oldnode = NULL;}
        
index ada2a8bdcb0c95c35c69a521381b68bda9ee13d1..4943972607dd36d763d8d3669217b3b2660b36d1 100644 (file)
@@ -35,7 +35,6 @@ extern int
 mps_find_icon_number_from_desc(const char *desc, 
        garmin_formats_e garmin_format);
 
-
 /* MapSource 4.13 */
 static icon_mapping_t icon_table[] = {
 /*       mps    pcx    desc */
index 306424330de42accca0a56702cde1bb1176ac677..82ec89d89678c65e4fe27a5d32c0d6a9dc989adf 100644 (file)
@@ -218,7 +218,6 @@ main(int argc, char *argv[])
                usage(argv[0]);
                exit(0);
        }
-
        if (ovecs == NULL)
                waypt_disp_all(waypt_disp);
 
index e9e1f2ff8e1b28ba68e26d5ca9d51519d0bc1c51..798455441a109ac5fb6c44ec03e29de4f9765772 100644 (file)
@@ -29,30 +29,12 @@ static FILE *mapsend_file_in;
 static FILE *mapsend_file_out;
 static void *mkshort_handle;
 
-static int endianness_tested;
-static int i_am_little_endian;
-
 static int route_wp_count;
 static int mapsend_infile_version;
 static int trk_version = 30;
 
 #define MYNAME "mapsend"
 
-static void
-test_endianness(void)
-{
-       union {
-                long l;
-                unsigned char uc[sizeof (long)];
-        } u;
-
-        u.l = 1;
-        i_am_little_endian = u.uc[0];
-
-       endianness_tested = 1;
-
-}
-
 static void
 mapsend_rd_init(const char *fname)
 {
@@ -69,103 +51,45 @@ mapsend_rd_deinit(void)
 }
 
 static 
-size_t 
+void 
 my_fread8(void *ptr, FILE *stream)
 {
        unsigned char cbuf[8];
-       unsigned char *cptr = ptr;
        size_t rv;
 
-       if (!endianness_tested) {
-               test_endianness();
-       }
-
-       if (i_am_little_endian) {       
-               rv = fread(ptr, 8, 1, stream);
-       } else { 
-               rv = fread(cbuf, 8, 1, stream);
-               cptr[0] = cbuf[7];
-               cptr[1] = cbuf[6];
-               cptr[2] = cbuf[5];
-               cptr[3] = cbuf[4];
-               cptr[4] = cbuf[3];
-               cptr[5] = cbuf[2];
-               cptr[6] = cbuf[1];
-               cptr[7] = cbuf[0];
-       }
-       return rv;
+       rv = fread(cbuf, 8, 1, stream);
+       le_read64(ptr, cbuf);
+       
 }
 
 static 
-size_t
+void
 my_fwrite8(void *ptr, FILE *stream)
 {
        unsigned char cbuf[8];
-       unsigned char *cptr = ptr;
 
-       if (!endianness_tested) {
-               test_endianness();
-       }
-
-       if (i_am_little_endian) {       
-               return fwrite(ptr, 8, 1, stream);
-       } else {
-               cbuf[0] = cptr[7];
-               cbuf[1] = cptr[6];
-               cbuf[2] = cptr[5];
-               cbuf[3] = cptr[4];
-               cbuf[4] = cptr[3];
-               cbuf[5] = cptr[2];
-               cbuf[6] = cptr[1];
-               cbuf[7] = cptr[0];
-               return fwrite(cbuf, 8, 1, stream);
-       }
+       le_read64(cbuf, ptr);
+       fwrite(cbuf, 8, 1, stream);
 }
 
 static 
-size_t 
+void 
 my_fread4(void *ptr, FILE *stream)
 {
+       unsigned int *iptr = ptr;
        unsigned char cbuf[4];
-       unsigned char *cptr = ptr;
        size_t rv;
-       
-       if (!endianness_tested) {
-               test_endianness();
-       }
 
-       if (i_am_little_endian) {       
-               rv = fread(ptr, 4, 1, stream);
-       } else {
-               rv = fread(cbuf, 4, 1, stream);
-               cptr[0] = cbuf[3];
-               cptr[1] = cbuf[2];
-               cptr[2] = cbuf[1];
-               cptr[3] = cbuf[0];
-       }
-       return rv;
+       rv = fread(cbuf, 4, 1, stream);
+       *iptr = le_read32(cbuf);
 }
 
 static 
 size_t
 my_fwrite4(int *ptr, FILE *stream)
 {
-       unsigned char cbuf[4];
-       unsigned char *cptr = (unsigned char *) ptr;
-
-       if (!endianness_tested) {
-               test_endianness();
-       }
-
-       if (i_am_little_endian) {       
-               return fwrite(ptr, 4, 1, stream);
-       } else {
-               cbuf[0] = cptr[3];
-               cbuf[1] = cptr[2];
-               cbuf[2] = cptr[1];
-               cbuf[3] = cptr[0];
-               return fwrite(cbuf, 4, 1, stream);
-       }
+       int i = le_read32(ptr);
+       return fwrite(&i, 4, 1, stream);
 }
 
 static void
@@ -528,7 +452,7 @@ void mapsend_track_hdr(const route_head * trk)
         * we write mapsend v3.0 tracks as mapsend v2.0 tracks get
         * tremendously out of whack time/date wise.
         */
-       char *verstring;
+       char *verstring = "30";
        queue *elem, *tmp;
        char *tname;
        unsigned char c;
index 43606ea5cdecd1856cfbd127c44a12da0db2a529..3f015f91cb5a20460fcf7fbaa621641ff7720564 100644 (file)
@@ -1499,7 +1499,7 @@ mps_write(void)
        char            recType;
        int                     reclen;
        int                     reclen2;
-       int                     tocopy;
+       unsigned int                    tocopy;
 
        unsigned char   copybuf[8192];
 
index 127d9c885f28a7177770e35016a3faf6b410518f..c70321d3c27819bdb13e3100d3debf6530fd4317 100644 (file)
@@ -205,11 +205,6 @@ nav_wr_deinit(void)
        fclose(ofd);
 }
 
-static void
-nav_waypt_pr(const waypoint *waypointp)
-{
-}
-
 void
 nav_write(void)
 {
index 2cc00b020abe4cf1d96ad7e7c61c24d19e59e85e..46b27a31f36365afca775f7c3c1a870384938cbb 100644 (file)
@@ -24,7 +24,6 @@
 
 extern queue waypt_head;
 
-static double pos_dist;
 static char *polyfileopt = NULL;
 static char *exclopt = NULL;
 
index 4aacd5cd9e8a1bdcb006060736c24df0707818e0..acc73597389edb6b9197d44964b8b687f29739ca 100644 (file)
 #include "defs.h"
 #include <ctype.h>
 
-#ifndef M_PI
-#  define M_PI 3.14159265358979323846
-#endif
-
 #define MYNAME "PSP"
 
 #define MAXPSPSTRINGSIZE       256
@@ -36,24 +32,6 @@ static FILE *psp_file_in;
 static FILE *psp_file_out;
 static FILE *mkshort_handle;
 
-static int i_am_little_endian;
-static int endianness_tested;
-
-static void
-test_endianness(void)
-{
-        union {
-                long l;
-                unsigned char uc[sizeof (long)];
-        } u;
-
-        u.l = 1;
-        i_am_little_endian = u.uc[0];
-
-        endianness_tested = 1;
-
-}
-
 static int
 psp_fread(void *buff, size_t size, size_t members, FILE * fp) 
 {
@@ -74,23 +52,9 @@ psp_fread_double(FILE *fp)
        unsigned char buf[8];
        unsigned char sbuf[8];
 
-       if (!endianness_tested) {
-               test_endianness();
-       }
-
-       psp_fread(buf, 1, 8, psp_file_in);
-       if (i_am_little_endian) {
-               return *(double *) buf;
-       }
-       sbuf[0] = buf[7];
-       sbuf[1] = buf[6];
-       sbuf[2] = buf[5];
-       sbuf[3] = buf[4];
-       sbuf[4] = buf[3];
-       sbuf[5] = buf[2];
-       sbuf[6] = buf[1];
-       sbuf[7] = buf[0];
-       return *(double *)sbuf;
+       psp_fread(buf, 1, 8, fp);
+       le_read64(sbuf, buf);
+       return *(double *) sbuf;
 }
 
 static void
@@ -99,46 +63,9 @@ psp_fwrite_double(double x, FILE *fp)
        unsigned char *cptr = (unsigned char *)&x;
        unsigned char cbuf[8];
 
-       if (!endianness_tested) {
-               test_endianness();
-       }
-       if (i_am_little_endian) {
-               fwrite(&x, 8, 1, fp);
-       } else {
-                cbuf[0] = cptr[7];
-                cbuf[1] = cptr[6];
-                cbuf[2] = cptr[5];
-                cbuf[3] = cptr[4];
-                cbuf[4] = cptr[3];
-                cbuf[5] = cptr[2];
-                cbuf[6] = cptr[1];
-                cbuf[7] = cptr[0];
-                fwrite(cbuf, 8, 1, fp);
-       }
-
+       le_read64(cbuf, cptr);
+       fwrite(cbuf, 8, 1, fp);
 }
-#if 0
-static void
-psp_fwrite_word(unsigned int x, FILE *fp)
-{
-       char *cptr = &x;
-       char *cbuf[4];
-
-       if (!endianness_tested) {
-               test_endianness();
-       }
-       if (i_am_little_endian) {
-               fwrite(&x, 4, 1, fp);
-       } else {
-                cbuf[0] = cptr[3];
-                cbuf[1] = cptr[2];
-                cbuf[2] = cptr[1];
-                cbuf[3] = cptr[0];
-                fwrite(cbuf, 4, 1, fp);
-       }
-}
-#endif
-
 
 /* Implement the grid in ascii art... This makes a bit of sense if you stand
    on a point over the north pole and look down on the earth.
index 74cfd57a00f42a5b8aca90612718a10c165161dd..6c85a3bf8d425208883f555c89b84637260d54d9 100644 (file)
@@ -134,19 +134,14 @@ void
 route_flush(queue *head)
 {
        queue *elem, *tmp;
-       route_head *last = NULL;
-       
+       queue *q;
+
        QUEUE_FOR_EACH(head, elem, tmp) {
-               if ( last ) {
-                       route_free(last);
-               }
-               last = (route_head *)elem;
-       }
-       if ( last ) {
-               route_free(last);
+               q = dequeue(elem);
+               route_free((route_head *) q);
        }
-       QUEUE_INIT(head);
 }
+
 void
 route_flush_all()
 {
index 165d2809ec0b14f00a0d9a24379ff0eeaeaecc30..ef2fbc622fedb31b51222e1a2e1ec2bead21c22b 100644 (file)
@@ -32,25 +32,8 @@ static FILE *tpg_file_in;
 static FILE *tpg_file_out;
 static void *mkshort_handle;
 
-static int i_am_little_endian;
-static int endianness_tested;
 static unsigned int waypt_out_count;
 
-static void
-test_endianness(void)
-{
-        union {
-                long l;
-                unsigned char uc[sizeof (long)];
-        } u;
-
-        u.l = 1;
-        i_am_little_endian = u.uc[0];
-
-        endianness_tested = 1;
-
-}
-
 static int
 tpg_fread(void *buff, size_t size, size_t members, FILE * fp) 
 {
@@ -71,22 +54,8 @@ tpg_fread_double(FILE *fp)
        unsigned char buf[8];
        unsigned char sbuf[8];
 
-       if (!endianness_tested) {
-               test_endianness();
-       }
-
        tpg_fread(buf, 1, 8, fp);
-       if (i_am_little_endian) {
-               return *(double *) buf;
-       }
-       sbuf[0] = buf[7];
-       sbuf[1] = buf[6];
-       sbuf[2] = buf[5];
-       sbuf[3] = buf[4];
-       sbuf[4] = buf[3];
-       sbuf[5] = buf[2];
-       sbuf[6] = buf[1];
-       sbuf[7] = buf[0];
+       le_read64(sbuf, buf);
        return *(double *)sbuf;
 }
 
@@ -96,23 +65,8 @@ tpg_fwrite_double(double x, FILE *fp)
        unsigned char *cptr = (unsigned char *)&x;
        unsigned char cbuf[8];
 
-       if (!endianness_tested) {
-               test_endianness();
-       }
-       if (i_am_little_endian) {
-               fwrite(&x, 8, 1, fp);
-       } else {
-                cbuf[0] = cptr[7];
-                cbuf[1] = cptr[6];
-                cbuf[2] = cptr[5];
-                cbuf[3] = cptr[4];
-                cbuf[4] = cptr[3];
-                cbuf[5] = cptr[2];
-                cbuf[6] = cptr[1];
-                cbuf[7] = cptr[0];
-                fwrite(cbuf, 8, 1, fp);
-       }
-
+       le_read64(cbuf, cptr);
+       fwrite(cbuf, 8, 1, fp);
 }
 
 static int
index 54c6267f7a588399da77c59d537027b36c8ca65d..879a291f94a9b91625a66d89ce653020762be4c2 100644 (file)
@@ -53,6 +53,7 @@ waypt_dupe(waypoint *wpt)
 
        return tmp;
 }
+
 void
 waypt_add(waypoint *wpt)
 {
@@ -190,21 +191,14 @@ void
 waypt_flush( queue *head )
 {
        queue *elem, *tmp;
-       waypoint *last = NULL;
-                       
+
+       queue *q;
        QUEUE_FOR_EACH(head, elem, tmp) {
-               if ( last ) {
-                       waypt_free(last);
-               }
-               last = (waypoint *)elem;
-        }
-       
-       if ( last ) {
-               waypt_free(last);
+               q = dequeue(elem);
+               waypt_free((waypoint *) q);
        }
-       
-       QUEUE_INIT(head);
 }
+
 void
 waypt_flush_all()
 {